package pl.edu.agh.hollonCommision.graph;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.edu.agh.hollonCommision.graph.connectionModel.IConnectionModel;

import dtp.commission.Commission;


public class GraphModel {
	
	private Map<Integer,Vertex> vertices;
	private Map<Integer, List<Edge> > edges;
	
	public GraphModel() {
		edges = new HashMap<Integer, List<Edge>>();
	}
	
	public void setVertices(Map<Integer,Vertex> vertices){
		this.vertices = vertices;
	}
	
	public void AddEdge(Vertex v1, Vertex v2){
		
		if(!edges.containsKey(v1.getID())){
			edges.put(v1.getID(), new ArrayList<Edge>());
		}
		Edge e = new Edge(v1,v2);
		v1.setMetricValue("degree", v1.getMetricValue("degree")+1);
		v2.setMetricValue("degree", v2.getMetricValue("degree")+1);
		edges.get(v1.getID()).add(e);
	}

	public static GraphModel getGraph(Commission[] comm, IConnectionModel connectionModel){
		GraphModel model = new GraphModel();
		Map<Integer, Vertex> vertices = new HashMap<Integer, Vertex>(); 
		for(Commission c:comm){
			Vertex v = new Vertex(c);
			vertices.put(c.getID(), v);
		}
		model.setVertices(vertices);
		for(int i =0; i < comm.length; i++){
			for(int j = i+1 ; j < comm.length; j++){
				if(i!=j && connectionModel.areConnected(comm[i],comm[j])){
					model.AddEdge(vertices.get(comm[i].getID()),vertices.get(comm[j].getID()));
				}
			}
		}
		
		return model;
	}

	public Map<Integer, Vertex> getVertices() {
		return vertices;
	}

	public Map<Integer, List<Edge>> getEdges() {
		return edges;
	}
}
